﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OrangePrj
{
    public partial class FormAddWork : Form
    {
        public Work w { get; set; }

        public FormAddWork(Work work)
        {
            InitializeComponent();
            this.w = work;
            comboInit();
            textBoxWorkName.Text = work.Name;
            textBoxWorkDescr.Text = work.Description;
            textBoxWorkRes.Text = work.Resource.Amount.ToString();
            textBoxWorkTime.Text = work.Time.ToString();
            comboBoxResourceType.SelectedItem = work.Resource.Type;
            comboBoxWorkType.SelectedItem = work.Type;
            this.Text = "Изменение работы";
            this.buttonJobAdd.Text = "Изменить";
        }
        public FormAddWork()
        {
            InitializeComponent();
            comboInit();
            comboBoxWorkType.SelectedIndex = 0;
            comboBoxResourceType.SelectedIndex = 0;
        }
        private void buttonJobAdd_Click(object sender, EventArgs e)
        {
            if (w == null)
                w = new Work();

            w.Name = textBoxWorkName.Text;
            w.Description = textBoxWorkDescr.Text;
            w.Type = (WorkType)comboBoxWorkType.SelectedItem;
            w.Resource.Type = (ResourceType)comboBoxResourceType.SelectedItem;

            if (textBoxWorkRes.Text == "")
            {
                w.Resource.Amount = 0;
            }
            else
            {
                w.Resource.Amount = Convert.ToDouble(textBoxWorkRes.Text);
            }

            if (textBoxWorkTime.Text == "")
            {
                w.Time = 0;
            }
            else
            {
                w.Time = Convert.ToDouble(textBoxWorkTime.Text);
            }
        }
        private void comboBoxWorkType_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
        private void textBoxWorkTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBoxWorkTime.Text.Contains(',') == true && e.KeyChar == ',')
                e.Handled = true;
            if (e.KeyChar != ',')
               if (char.IsDigit(e.KeyChar) == false)
                   if (e.KeyChar != '\b')
                       e.Handled = true;
        }
        private void textBoxWorkRes_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBoxWorkRes.Text.Contains(',') == true && e.KeyChar == ',')
            {
                e.Handled = true;
            }
            if (e.KeyChar != ',')
                if (char.IsDigit(e.KeyChar) == false)
                    if (e.KeyChar != '\b')
                        e.Handled = true;
        }
        private void comboInit()
        {
            comboBoxResourceType.Items.AddRange(new object[] { ResourceType.Объём, ResourceType.Интенсивность });
            comboBoxWorkType.Items.AddRange(new object[] { WorkType.Простая, WorkType.ИЛИ, WorkType.Искл_ИЛИ, WorkType.Возврат });
        }
    }
}
